import { createRouter, createWebHistory } from 'vue-router'
import PkIndexView from '../views/pk/PkIndexView.vue'
import RankListIndexView from '../views/ranklist/RankListIndexView.vue'
import RecordIndexView from '../views/record/RecordIndexView.vue'
import NotFound from '../views/error/NotFound.vue'
import UserBotIndexView from '../views/user/bots/UserBotIndexView.vue'
import UserAccountLoginView from "@/views/user/account/UserAccountLoginView";
import UserAccountRegisterView from "@/views/user/account/UserAccountRegisterView";
import RecordContentView from "@/views/record/RecordContentView";
/* import { TrackOpTypes } from 'vue' */
import store from '@/store/index'

const routes = [
  {
      path:"/",
      name:"home",
      redirect:"/pk/",
      meta: { // 表示是否需要授权
        requestAuth: true,
      }
  },
  {
    path: "/pk/",
    name: "pk_index",
    component:PkIndexView,
    meta: { // 表示是否需要授权
      requestAuth: true,
    }
  },
  {
    path: "/record/",
    name: "record_index",
    component:RecordIndexView,
    meta: { // 表示是否需要授权
      requestAuth: true,
    }
  },
  {
    path: "/record/:recordId/",
    name: "record_content",
    component:RecordContentView,
    meta: { // 表示是否需要授权
      requestAuth: true,
    }
  },
  {
    path: "/ranklist/",
    name: "ranklist_index",
    component:RankListIndexView,
    meta: { // 表示是否需要授权
      requestAuth: true,
    }
  },
  {
    path: "/user/bots/",
    name: "user_bot_index",
    component:UserBotIndexView,
    meta: { // 表示是否需要授权
      requestAuth: true,
    }
  },
  {
    path: "/user/account/login/",
    name: "user_account_login",
    component: UserAccountLoginView,
    meta: { // 表示是否需要授权
      requestAuth: false,
    }
  },
  {
    path: "/user/account/register/",
    name: "user_account_register",
    component: UserAccountRegisterView,
    meta: { // 表示是否需要授权
      requestAuth: false,
    }
  },

  {
    path: "/404/",
    name: "404_index",
    component:NotFound,
  },

  {
    path:"/:catchAll(.*)",
    redirect: "/404/",
  },


]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => { // 实现前端业面的授权, to跳转到哪个业面，from从哪个业面跳转过去，next判断业面是否执行下一步操作
  if (to.meta.requestAuth && !store.state.user.is_login) { // 若需要授权且用户未登录
    next({name: "user_account_login"}); // 跳转到登录业面
  } else {
    next(); // 跳转到默认业面
  }
})
export default router
